package com.secsys.util;

import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.secsys.model.TbBbsModule;

public class MySqlUtil {
	private static final Logger logger = Logger.getLogger(MySqlUtil.class);
    /**
     * 
     * @Description BBS 基本搜索
     * @param searchType
     * @param bean
     * @return
     */
	public static String basicMatchHQL(String searchType, 
			String bean) {
		StringBuffer hql = new StringBuffer("from ");
		hql.append(bean);
		hql.append(" as " + Constant.POST_SORTNAME);
		hql.append(" where");
	    if(searchType.equals(Constant.SEARCH_AUTHOR)){   //作者
	    	hql.append(" "+Constant.POST_SORTNAME+"."+Constant.TBBSUSER+"= ?");
	    }else if(searchType.equals(Constant.SEARCH_CONTENT)){   //内容
	    	 hql.append(" "+Constant.POST_SORTNAME+".content like ?");
	    }else if(searchType.equals(Constant.SEARCH_TITLE)){   //标题
	    	hql.append(" "+Constant.POST_SORTNAME+".title like ?" );
	    }
 
		return hql.toString();
	}
	/**
	 * 
	 * @Description like模糊匹配拼装 
	 * @param keyWord
	 * @return
	 */
	public static String likeMatchParaten(String keyWord){
		 StringBuffer temp=new StringBuffer("%");
		 temp.append(keyWord);
		 temp.append("%");
		 return temp.toString();
	}
	
	/**
	 * 
	 * @Description like模糊匹配拼装 
	 * @param keyWord
	 * @return
	 */
	public static String likeRightMatchParaten(String keyWord){
		 StringBuffer temp=new StringBuffer(" '");
		 temp.append(keyWord);
		 temp.append("%'");
		 return temp.toString();
	}
	/**
	 * 
	 * @Description BBS高级搜索
	 * @param searchType
	 * @param bean
	 * @param searchTime1
	 * @param searchTime2
	 * @param timeSort
	 * @param searchScope
	 * @return
	 */
	public static String advMatchHQL(String searchType, 
			String bean,String searchTime1,String searchTime2,String timeSort,TbBbsModule module){
		Date date=new Date();
		StringBuffer hql = new StringBuffer("FROM ");
		hql.append(bean);
		hql.append(" AS " + Constant.POST_SORTNAME);
		hql.append(" WHERE");
	    if(searchType.equals(Constant.SEARCH_AUTHOR)){   //作者
	    	hql.append(" "+Constant.POST_SORTNAME+"."+Constant.TBBSUSER+"= ?");
	    }else if(searchType.equals(Constant.SEARCH_CONTENT)){   //内容
	    	 hql.append(" "+Constant.POST_SORTNAME+".content like ?");
	    }else if(searchType.equals(Constant.SEARCH_TITLE)){   //标题
	    	hql.append(" "+Constant.POST_SORTNAME+".title like ?" );
	    }else if(searchTime1.equals("1_day")){
	    	          
	    	 if(searchTime2.equals("bf")){   
	    		 long tempTime=date.getTime()-DateFormatUtils.getDateTime(); 
	    		   hql.append(" and  "+Constant.POST_SORTNAME+".publishTime between '"+DateFormatUtils.convertDateToString(new Date(tempTime))+"' and '"+DateFormatUtils.convertDateToString(date)+"'");
	    	   }else{
	    		   hql.append(" and  "+Constant.POST_SORTNAME+".publishTime<'"+DateFormatUtils.convertDateToString(new Date())+"'");  
	    	   }
	    }else if(searchTime1.equals("2_day")){
	    	 if(searchTime2.equals("bf")){   
	    		 long tempTime=date.getTime()-2*DateFormatUtils.getDateTime();
	    		 hql.append(" and  "+Constant.POST_SORTNAME+".publishTime between '"+DateFormatUtils.convertDateToString(new Date(tempTime))+"' and '"+DateFormatUtils.convertDateToString(date)+"'");
	    	   }else{
	    		   hql.append(" and  "+Constant.POST_SORTNAME+".publishTime<'"+DateFormatUtils.convertDateToString(new Date())+"'");  
	    	   }
	    }else if(searchTime1.equals("1_week")){
	    	 if(searchTime2.equals("bf")){   
	    		 long tempTime=date.getTime()-7*DateFormatUtils.getDateTime();
	    		 hql.append(" and  "+Constant.POST_SORTNAME+".publishTime between '"+DateFormatUtils.convertDateToString(new Date(tempTime))+"' and '"+DateFormatUtils.convertDateToString(date)+"'");
	    	   }else{
	    		   hql.append(" and  "+Constant.POST_SORTNAME+".publishTime<'"+DateFormatUtils.convertDateToString(new Date())+"'");  
	    	   }
	    }else if(searchTime1.equals("1_month")){
	    	  
	    }else if(searchTime1.equals("3_month")){
	    	              
	    }else if(searchTime1.equals("6_month")){
	    	
	    }else if(searchTime1.equals("01_year")){
	    	
	    } else if(module!=null) {
	    	 hql.append(" and "+Constant.POST_SORTNAME+".tbBbsModule="+module);      
	    } else if(timeSort.equals("asc")){
	    	 hql.append(" order by "+Constant.POST_SORTNAME+".publishTime"+" ASC"); 
	    }else if(timeSort.equals("desc")){
	    	 hql.append(" order by "+Constant.POST_SORTNAME+".publishTime"+" DESC"); 
	    }
	    return hql.toString();
	}
	

}
